#include "stdafx.h"
#include "QMemFile.h"


stu_QFileHead::stu_QFileHead()
{
	nVersion = 1;
	nFileType = 0;
	ZeroMemory(sFileNote,sizeof(sFileNote));
	nFileSize = 0;
	nCount = 0;
	nCountVer = 0;
	nFlag = 0xFEFE;
}

int stu_QFileHead::getsize()
{
	return sizeof(int) * 6 + sizeof(TCHAR) * FILEHEAD_FILENOTE;
}

bool stu_QFileHead::write(byte* &pBuffer,int &nSize)
{
	QMemFile file(pBuffer,getsize());
	write(file,nSize);
	file.detach();
	pBuffer += nSize;
	return true;
}

bool stu_QFileHead::write(QMemFile& fileWrite,int &nSize)
{
	nSize = 0;
	fileWrite.write(&nVersion,sizeof(int));
	fileWrite.write(&nFileType,sizeof(int));
	fileWrite.write(&sFileNote,sizeof(TCHAR) * FILEHEAD_FILENOTE);
	fileWrite.write(&nFileSize,sizeof(int));
	fileWrite.write(&nCount,sizeof(int));
	fileWrite.write(&nCountVer,sizeof(int));

	int nTemp = FILENODE_FLAG;
	fileWrite.write(&nTemp,sizeof(int));

	nSize = getsize();

	return true;
}

bool stu_QFileHead::read(byte* &pBuffer,int &nSize)
{
	QMemFile file(pBuffer,getsize());
	read(file,nSize);
	file.detach();
	pBuffer += nSize;
	return true;
}

bool stu_QFileHead::read(QMemFile& fileWrite,int &nSize)
{
	fileWrite.read(&nVersion,sizeof(int));
	fileWrite.read(&nFileType,sizeof(int));
	fileWrite.read(&sFileNote,sizeof(TCHAR) * FILEHEAD_FILENOTE);
	fileWrite.read(&nFileSize,sizeof(int));
	fileWrite.read(&nCount,sizeof(int));
	fileWrite.read(&nCountVer,sizeof(int));
	fileWrite.read(&nFlag,sizeof(int));
	if(nFlag != FILENODE_FLAG)
	{
		Q_ASSERT(0);
		return false;
	}
	nSize = getsize();

	return true;
}

stu_QFileNode::stu_QFileNode()
{
	reset();
}

void stu_QFileNode::reset()
{
	nFlag      = 0xFEFE;
	nVersion   = 0;      
	nDataType  = 0;     
	nBeginPos  = 0;     
	nEndPos    = 0;   
	lNumber     = 0;
	lNumberV    = 0;
	pWriteBuffer = NULL;
}

int stu_QFileNode::getsize()
{
	return sizeof(int) *5 + sizeof(long) * 2 + sizeof(int);
}

bool stu_QFileNode::write(byte* &pBuffer,int &nSize)
{
	nSize = 0;
	QMemFile file(pBuffer,getsize());
	BOOL bDone = write(file,nSize);
	file.detach();
	return true;
}

bool stu_QFileNode::write(QMemFile &fileWrite,int &nSize)
{
	nSize = 0;
	fileWrite.write(&nFlag,sizeof(int));
	fileWrite.write(&nVersion,sizeof(int));
	fileWrite.write(&nDataType,sizeof(int));
	fileWrite.write(&nBeginPos,sizeof(int));
	fileWrite.write(&nEndPos,sizeof(int));
	fileWrite.write(&lNumber,sizeof(long) );
	fileWrite.write(&lNumberV,sizeof(long));

	int dwTemp = FILENODE_FLAG;
	fileWrite.write(&dwTemp,sizeof(int));
	nSize = getsize();
	return true;
}

bool stu_QFileNode::read(QMemFile &fileRead,int &nSize)
{
	nSize = 0;
	fileRead.read(&nFlag,sizeof(int));
	fileRead.read(&nVersion,sizeof(int));
	fileRead.read(&nDataType,sizeof(int));
	fileRead.read(&nBeginPos,sizeof(int));
	fileRead.read(&nEndPos,sizeof(int));
	fileRead.read(&lNumber,sizeof(long) );
	fileRead.read(&lNumberV,sizeof(long));

	int nTemp = 0;
	fileRead.read(&nTemp,sizeof(int));
	if(nTemp != FILENODE_FLAG)
	{
		Q_ASSERT(0);
		return FALSE;
	}

	nSize = getsize();
	return true;
}

bool stu_QFileNode::read(byte* &pBuffer,int &nSize)
{
	nSize =0;
	QMemFile file(pBuffer,getsize());
	bool bDone = read(file,nSize);
	file.detach();
	return bDone;
}

bool stu_QFileNode::beginwrite(byte* &pBuffer,int &nSize)
{
	pWriteBuffer = pBuffer;
	nSize = getsize();
	return true;
}

bool stu_QFileNode::endwrite()
{
	if(!pWriteBuffer) return FALSE;

	int nSize =0;
	return write(pWriteBuffer,nSize);
}

bool stu_QFileNode::endwrite(int &nSize)
{
	nSize = 0;
	if(!pWriteBuffer) return FALSE;
	return write(pWriteBuffer,nSize);
}
